#!/bin/sh

PTZ_CONF_FILE="/etc/ptz_presets.conf"

usage() {
	echo "Usage: $0 [-g] [-a <preset_number> <preset_name> [<x_coord> <y_coord>]] [-r <preset_number>]
	-g: Display all presets
	-a: Add or overwrite a preset with number, name, and optionally coordinates.
	    Use -1 as preset_number to auto-select an empty slot (based on missing coordinates).
	-r: Remove a preset by number
	<preset_number>: Run the specified preset
" >&2
	exit 1
}

if [ $# -lt 1 ]; then
	usage
fi

case "$1" in
	-g)
		while IFS='=,' read -r PNUM PNAME PX PY; do
			[ "${PNUM#\#}" != "$PNUM" ] || echo "$PNUM=$PNAME,$PX,$PY"
		done < "$PTZ_CONF_FILE"
		exit 0
		;;
	-a)
		if [ $# -lt 3 ]; then
			echo "Error: Missing arguments for -a." >&2
			usage
		fi

		INPUT_NUM=$2
		PRESET_NAME=$3

		if [ $# -eq 5 ]; then
			PRESET_X=$4
			PRESET_Y=$5
		else
			MOTOR_POS=$(motors -p)
			PRESET_X=$(echo "$MOTOR_POS" | cut -d',' -f1)
			PRESET_Y=$(echo "$MOTOR_POS" | cut -d',' -f2)
			[ -z "$PRESET_X" ] || [ -z "$PRESET_Y" ] && echo "Error: Failed to get coordinates from motors -p" && exit 1
		fi

		if [ "$INPUT_NUM" = "-1" ]; then
			# Find the first preset with missing coordinates
			TEMP_FILE=$(mktemp)
			USED=0
			while IFS= read -r line; do
				case "$line" in
					\#*) echo "$line" >> "$TEMP_FILE" ;;
					*)
						PNUM=$(echo "$line" | cut -d'=' -f1)
						VALS=$(echo "$line" | cut -d'=' -f2)
						PX=$(echo "$VALS" | cut -d',' -f2)
						PY=$(echo "$VALS" | cut -d',' -f3)
						if [ "$USED" -eq 0 ] && { [ -z "$PX" ] || [ -z "$PY" ]; }; then
							echo "$PNUM=$PRESET_NAME,$PRESET_X,$PRESET_Y" >> "$TEMP_FILE"
							USED=1
							PRESET_NUM=$PNUM
						else
							echo "$line" >> "$TEMP_FILE"
						fi
						;;
				esac
			done < "$PTZ_CONF_FILE"

			if [ "$USED" -eq 0 ]; then
				PRESET_NUM=$(awk -F= '/^[0-9]+=/ {print $1}' "$PTZ_CONF_FILE" | sort -n | tail -n1)
				PRESET_NUM=$((PRESET_NUM + 1))
				echo "$PRESET_NUM=$PRESET_NAME,$PRESET_X,$PRESET_Y" >> "$TEMP_FILE"
			fi

			mv "$TEMP_FILE" "$PTZ_CONF_FILE"
			echo "Preset $PRESET_NUM added or updated."
		else
			PRESET_NUM=$INPUT_NUM
			if grep -q "^$PRESET_NUM=" "$PTZ_CONF_FILE"; then
				sed -i "s/^$PRESET_NUM=.*/$PRESET_NUM=$PRESET_NAME,$PRESET_X,$PRESET_Y/" "$PTZ_CONF_FILE"
				echo "Preset $PRESET_NUM updated."
			else
				echo "$PRESET_NUM=$PRESET_NAME,$PRESET_X,$PRESET_Y" >> "$PTZ_CONF_FILE"
				echo "Preset $PRESET_NUM added."
			fi
		fi
		exit 0
		;;
	-r)
		[ $# -ne 2 ] && echo "Error: Missing preset number." && usage
		PRESET_NUM=$2
		if grep -q "^$PRESET_NUM=" "$PTZ_CONF_FILE"; then
			sed -i "s/^$PRESET_NUM=.*/$PRESET_NUM=,,/" "$PTZ_CONF_FILE"
			echo "Preset $PRESET_NUM removed."
		else
			echo "Preset $PRESET_NUM not found."
		fi
		exit 0
		;;
	*)
		PRESET_NUM=$1
		while IFS='=,' read -r PNUM PNAME PX PY; do
			[ "${PNUM#\#}" != "$PNUM" ] && continue
			if [ "$PNUM" = "$PRESET_NUM" ]; then
				if [ -n "$PX" ] && [ -n "$PY" ]; then
					logger -t ptz_presets "Running: motors -d h -x $PX -y $PY"
					motors -d h -x "$PX" -y "$PY"
					exit 0
				else
					echo "Invalid preset or missing coordinates for $PRESET_NUM."
					exit 1
				fi
			fi
		done < "$PTZ_CONF_FILE"
		echo "Preset $PRESET_NUM not found."
		exit 1
		;;
esac
